package com.cow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cow.dao.OrderMapper;
import com.cow.dao.ProductMapper;
import com.cow.dao.ShoppingCartMapper;
import com.cow.entity.Order;
import com.cow.entity.Product;
import com.cow.entity.ShoppingCart;
import com.cow.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public boolean handleCartOrder(List<Order> orders) {
        //下单需要做哪些事情？
        //1.判断商品数量是否充足，如果充足修改库存，判断是否缺货修改缺货状态，修改上架状态
        //2.添加一条订单记录
        //3.刪除已購買的購物車信息

        for (Order order : orders) {
            Product product = productMapper.selectOne(new QueryWrapper<Product>().eq("product_no", order.getProductNo()));
            if (product.getProductStock() > order.getPayAmount()) {
                int stock = product.getProductStock() - order.getPayAmount();
                product.setProductStock(stock);
                product.setIsStockOut(stock < product.getLowestStock());
                product.setIsSale(stock > 0);
                productMapper.updateById(product);
                orderMapper.insert(order);
                shoppingCartMapper.delete(new QueryWrapper<ShoppingCart>().eq("product_id",product.getProductId()));
            }
        }


        return true;
    }

    @Override
    public boolean add(Order order) {
        Product product = productMapper.selectOne(new QueryWrapper<Product>().eq("product_no", order.getProductNo()));
        if (product.getProductStock() > order.getPayAmount()) {
            int stock = product.getProductStock() - order.getPayAmount();
            product.setProductStock(stock);
            product.setIsStockOut(stock < product.getLowestStock());
            product.setIsSale(stock > 0);
            productMapper.updateById(product);
            orderMapper.insert(order);
        }
        return true;
    }

    @Override
    public List<Map<String, Object>> selectOrdersByUserAccount(String userAccount) {
        return orderMapper.selectOrdersByUserAccount(userAccount);
    }

    @Override
    public Map<String, Object> orderOverView() {
        return orderMapper.orderOverView();
    }

    @Override
    public List<Map<String, Object>> selectProductTypeChart(String startTime, String endTime) {
        return orderMapper.selectProductTypeChart(startTime,endTime);
    }

    @Override
    public List<Map<String, Object>> selectChartDate(String startTime, String endTime) {
        return orderMapper.selectChartDate(startTime,endTime);
    }

    @Override
    public Map<String, Object> selectCountAndAmount(String startTime, String endTime) {
        return orderMapper.selectCountAndAmount(startTime,endTime);
    }
}
